using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
/// <summary>
/// 字符串连接与分割
/// </summary>
public static class HFStringJoinSplitExtend
{
    /// <summary>
    /// 连符号与AliasTooltipAttribute映射
    /// </summary>
    static readonly Dictionary<int, string> mJoinSymbolMaping =
        typeof(enHFSeparatorSymbol).HFValueToSpecialValue<int, HFAliasTooltipAttribute, string>((v, a) => { return a.alias; });

    /// <summary>
    /// 分割符号与AliasTooltipAttribute映射
    /// </summary>
    static readonly Dictionary<int, string> mSplitSymbolMaping =
        typeof(enHFSeparatorSymbol).HFValueToSpecialValue<int, HFAliasTooltipAttribute, string>((v, a) => { return a.alias; });

    /// <summary>
    /// 组装分隔符
    /// </summary>
    /// <param name="_symbol">分隔符</param>
    /// <returns>分隔符组</returns>
    static string[] OnSplitSymbol(enHFSeparatorSymbol _symbol)
    {
        List<string> result = new List<string>();
        foreach (KeyValuePair<int, string> key in mSplitSymbolMaping)
        {
            if ((key.Key & (int)_symbol) == key.Key)
            {
                if (!result.Contains(key.Value))
                {
                    result.Add(key.Value);
                }                
            }
        }
        return result.ToArray();
    }
    #region 获得符号字符
    /// <summary>
    /// 获得符号字符
    /// </summary>
    /// <param name="_symbol">符号</param>
    /// <returns>符号字符</returns>
    public static string HFSymbol(this enHFSeparatorSymbol _symbol)
    { 
        return mJoinSymbolMaping[(int)_symbol];
    }
    #endregion

    #region Join
    /// <summary>
    /// 连接字符串
    /// </summary>
    /// <param name="_strings">字符组</param>
    /// <param name="_symbol">连接符</param>
    /// <returns>连接后的字符串</returns>
    public static string HFJoin<T>(this IEnumerable<T> _strings, enHFSeparatorSymbol _symbol)
    {
        return string.Join(mJoinSymbolMaping[(int)_symbol], _strings);
    }
    #endregion

    #region Split
    /// <summary>
    /// 分割字符结果缓存
    /// </summary>
    static readonly Dictionary<int, string[]> mSplitCacheResultMaping = new Dictionary<int, string[]>();
    /// <summary>
    /// 切割空结果
    /// </summary>
    static readonly string[] mNullSplitResult = new string[0];
    /// <summary>
    /// 分割字符串 转Int
    /// </summary>
    /// <param name="_value">字符</param>
    /// <param name="_symbol">分割符</param>
    /// <returns></returns>
    public static List<int> HFSplitToInt(this string _value, enHFSeparatorSymbol _symbol)
    {
        string[] sp = HFSplit(_value, _symbol);
        List<int> spInt = new List<int>();
        for (int i = 0; i < sp.Length; i++)
        {
            int tampInt;
            if (int.TryParse(sp[i], out tampInt))
            {
                spInt.Add(tampInt);
            }
            else
            {
                HFP.HFError($"当前String：{_value} 转换失败!!!");
            }

        }
        return spInt;
    }
    /// <summary>
    /// 分割字符串
    /// </summary>
    /// <param name="_string">字符</param>
    /// <param name="_symbol">分割符</param>
    /// <returns>字符组</returns>
    public static string[] HFSplit(this string _string, enHFSeparatorSymbol _symbol)
    {
        return _string.HFSplit(_symbol, StringSplitOptions.RemoveEmptyEntries);
    }

    /// <summary>
    /// 分割字符串
    /// </summary>
    /// <param name="_string">字符</param>
    /// <param name="_symbol">分割符</param>
    /// <param name="_options">选项</param>
    /// <returns>字符组</returns>
    public static string[] HFSplit(this string _string, enHFSeparatorSymbol _symbol, StringSplitOptions _options)
    {
        return _string.HFSplit(_symbol, _options, false);
    }

    /// <summary>
    /// 分割字符串
    /// </summary>
    /// <param name="_string">字符</param>
    /// <param name="_symbol">分割符</param>
    /// <param name="_isCacheResult">是否缓存结果</param>
    /// <returns>字符组</returns>
    public static string[] HFSplit(this string _string, enHFSeparatorSymbol _symbol,bool _isCacheResult)
    {
        return _string.HFSplit(_symbol, StringSplitOptions.RemoveEmptyEntries, _isCacheResult);
    }

    /// <summary>
    /// 分割字符串
    /// </summary>
    /// <param name="_string">字符</param>
    /// <param name="_symbol">分割符</param>
    /// <param name="_options">选项</param>
    /// <param name="_isCacheResult">是否缓存结果</param>
    /// <returns>字符组</returns>
    public static string[] HFSplit(this string _string, enHFSeparatorSymbol _symbol, StringSplitOptions _options, bool _isCacheResult)
    {
        string[] result = null;
        if (_isCacheResult)
        {
            _string = _string.Trim();
            int key = _string.OnGetSplitCacheKey();
            if (!mSplitCacheResultMaping.TryGetValue(key, out result))
            {
                result = _string.Split(OnSplitSymbol(_symbol), _options);
                mSplitCacheResultMaping[key] = result;
            }
        }
        else
        {
            result = _string.Split(OnSplitSymbol(_symbol), _options);
        }
        return result == null ? mNullSplitResult : result;
    }

    /// <summary>
    /// 清除分割缓存结果
    /// </summary>
    public static void ClearSplitCacheResult()
    {
        OnClearSplitCacheResult(string.Empty,true);
    }

    /// <summary>
    /// 清除分割缓存结果
    /// </summary>
    /// <param name="_string">字符</param>
    public static void ClearSplitCacheResult(string _string)
    {
        OnClearSplitCacheResult(_string, false);        
    }

    /// <summary>
    /// 清除分割缓存结果
    /// </summary>
    /// <param name="_string">字符</param>
    /// <param name="_clearAll">是否清除所有</param>
    static void OnClearSplitCacheResult(string _string, bool _clearAll)
    {
        if (_clearAll)
        {
            mSplitCacheResultMaping.Clear();
        }
        else
        {
            int key = _string.GetHashCode();
            mSplitCacheResultMaping.Remove(_string.OnGetSplitCacheKey());
        }        
    }

    /// <summary>
    /// 获得字符的切割缓存Key
    /// </summary>
    /// <param name="_string">字符</param>
    /// <returns>缓存Key</returns>
    static int OnGetSplitCacheKey(this string _string)
    {
        return _string.GetHashCode();
    }
    #endregion

    #region ToSummary 转换为Summary形式
    /// <summary>
    /// 转换描述为Summary形式
    /// </summary>
    /// <param name="_desc">描述</param>
    /// <returns>描述</returns>
    public static string HFToSummary(this string _desc)
    {
        StringBuilder sbSummary = new StringBuilder();
        string line = string.Empty;
        int num = 0;
        sbSummary.AppendLine("/// <summary>");
        if (string.IsNullOrEmpty(_desc))
        {
            sbSummary.AppendLine("\t/// ");
        }
        else
        {
            StringReader reader = new StringReader(_desc);
            do
            {
                line = reader.ReadLine();
                if (!string.IsNullOrEmpty(line))
                {
                    sbSummary.AppendLine("\t/// " + line);
                    num++;
                }
            } while (!string.IsNullOrEmpty(line));
        }
        sbSummary.AppendLine("\t/// </summary>");
        return sbSummary.ToString();
    }
    #endregion
}

/// <summary>
/// 分隔符
/// </summary>
public enum enHFSeparatorSymbol
{
    /// <summary>
    /// 逗号【,】
    /// </summary>
    [HFAliasTooltip(",")]
    Comma = 1 << 0,
    /// <summary>
    /// 下划线【_】
    /// </summary>
    [HFAliasTooltip("_")]
    Underline = 1 << 1,
    /// <summary>
    /// 竖线【|】
    /// </summary>
    [HFAliasTooltip("|")]
    VerticalBar = 1 << 2,
    /// <summary>
    /// 横杠【-】
    /// </summary>
    [HFAliasTooltip("-")]
    HorizontalBar = 1 << 3,
    /// <summary>
    /// 冒号【;】
    /// </summary>
    [HFAliasTooltip(";")]
    Colon = 1 << 4,
    /// <summary>
    /// 空格【 】
    /// </summary>
    [HFAliasTooltip(" ")]
    Space = 1 << 5,
    /// <summary>
    /// 空字符【】
    /// </summary>
    [HFAliasTooltip("")]
    Empty = 1 << 6,
    /// <summary>
    /// 加号【+】
    /// </summary>
    [HFAliasTooltip("+")]
    Plus = 1 << 7,
    /// <summary>
    /// 回车换行【\r\n】
    /// </summary>
    [HFAliasTooltip("\r\n")]
    NewLine = 1 << 8,
    /// <summary>
    /// 反斜杠【/】
    /// </summary>
    [HFAliasTooltip("/")]
    BackSlash = 1 << 9,
    /// <summary>
    /// 点点反斜杠【../】
    /// </summary>
    [HFAliasTooltip("../")]
    DoubleDotBackSlash = 1 << 10,
    /// <summary>
    /// 反单引号【`】
    /// </summary>
    [HFAliasTooltip("`")]
    Backquote = 1 << 11,
}